package com.zq.api.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.zq.common.annotation.Log;
import com.zq.common.core.domain.Result;
import com.zq.common.core.domain.entity.UserInfo;
import com.zq.common.core.domain.model.WeChatLoginUser;
import com.zq.common.core.redis.RedisCache;
import com.zq.common.enums.BusinessEnum;
import com.zq.common.enums.BusinessType;
import com.zq.common.enums.OperatorType;
import com.zq.common.exception.base.BaseException;
import com.zq.common.utils.StringUtils;
import com.zq.server.api.dao.UserInfoMapper;
import com.zq.server.api.domain.dto.SendCodeDto;
import com.zq.server.api.domain.dto.UserPhoneLoginDto;
import com.zq.server.api.domain.dto.VerifyPhoneCodeDto;
import com.zq.server.api.domain.dto.userWxLoginDto;
import com.zq.server.api.domain.vo.UserPhoneLoginVo;
import com.zq.server.api.domain.vo.userWxLoginVo;
import com.zq.server.api.service.ApiUserInfoService;
import com.zq.server.core.sevcice.weChatTokenService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Super
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理接口")
public class ApiUserController {
    @Autowired
    private ApiUserInfoService userInfoService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private weChatTokenService weChatTokenServices;
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 修改用户信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    @ApiOperation("修改用户信息")
    @PostMapping("/updateUserInfo")
    @Log(title = "获取用户信息", businessType = BusinessType.OTHER, operatorType = OperatorType.MOBILE)
    public Result updateUserInfo(@RequestBody UserInfo user) {
        if (StringUtils.isNull(user)) {
            throw new BaseException(BusinessEnum.BODY_NOT_MATCH);
        }
        return userInfoService.updateUserInfo(user);
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    @Log(title = "获取用户信息", businessType = BusinessType.OTHER, operatorType = OperatorType.MOBILE)
    public UserInfo getUserInfo(HttpServletRequest request) {
        try {
            String responseToken = weChatTokenServices.getToken(request);
            if (StringUtils.isNotNull(responseToken)) {
                WeChatLoginUser weChatUser = weChatTokenServices.getWeChatUser(responseToken);
                if (StringUtils.isNotNull(weChatUser.getUser())) {
                    return weChatUser.getUser();
                }
            } else {
                throw new BaseException("获取用户异常");
            }
        } catch (Exception e) {
            log.error("获取用户信息发生异常", e);
            throw new BaseException("获取用户异常");
        }
        return null;
    }

    /**
     * 微信登录
     * 
     * @param userWxLoginDto 微信登录DTO
     * @return 微信登录结果
     */
    @PostMapping("wxLogin")
    @ApiOperation("微信登录")
    @Log(title = "微信登录", businessType = BusinessType.OTHER, operatorType = OperatorType.MOBILE)
    public Result<userWxLoginVo> wxLogin(@RequestBody @Validated userWxLoginDto userWxLoginDto) {
        if (StringUtils.isNull(userWxLoginDto)) {
            log.error("微信登录发生异常");
            throw new BaseException(BusinessEnum.WX_USER_LOGIN_ERROR);
        }
        return userInfoService.weChatLogin(userWxLoginDto);
    }

    /**
     * 获取用户手机号
     * 
     * @param userPhoneLoginDto 用户手机号登录DTO
     * @return 用户手机号登录结果
     */
    @PostMapping("getUserPhone")
    @ApiOperation("获取手机号")
    public Result<UserPhoneLoginVo> getUserPhoneLogin(@RequestBody UserPhoneLoginDto userPhoneLoginDto)
        throws IOException {
        try {
            UserPhoneLoginVo userPhoneLoginVo = userInfoService.getUserPhone(userPhoneLoginDto);
            return Result.success(userPhoneLoginVo);
        } catch (IOException e) {
            log.error("获取用户手机号发生IO异常", e);
            return Result.error("获取用户手机号失败");
        }
    }

    /**
     * 发送验证码
     * 
     * @param sendCodeDto 发送验证码DTO
     * @return 发送验证码结果
     */
    @ApiOperation(value = "发送验证码")
    @PostMapping("/sendCode")
    public Result sendCode(@RequestBody SendCodeDto sendCodeDto) {
        if (StringUtils.isNull(sendCodeDto)) {
            log.error("发送验证码发生异常");
            return Result.error(BusinessEnum.SEND_PHONE_CODE_ERROR);
        }
        try {
            return userInfoService.sendVerificationCode(sendCodeDto);
        } catch (Exception e) {
            log.error("发送验证码发生异常", e);
            return Result.error(BusinessEnum.SEND_PHONE_CODE_ERROR);
        }
    }

    /**
     * 用户手机号验证登录
     * 
     * @param verifyPhoneCodeDto 手机号验证登录DTO
     * @return 验证结果
     */
    @ApiOperation(value = "手机号验证并登录")
    @PostMapping("/verifyPhoneCode")
    @Log(title = "手机号验证并登录", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    public Result verifyPhoneCode(@RequestBody @Valid VerifyPhoneCodeDto verifyPhoneCodeDto) {
        if (StringUtils.isNull(verifyPhoneCodeDto)) {
            log.error("手机号验证并登录发生异常");
            return Result.error("手机号验证并登录失败");
        }
        try {
            return userInfoService.verifyPhoneCode(verifyPhoneCodeDto);
        } catch (Exception e) {
            log.error("手机号验证并登录发生异常", e);
            return Result.error("手机号验证并登录失败");
        }
    }

}
